package cobersky.skinUI.core
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;

	public class Component extends Sprite
	{
		protected var _listeners:Vector.<Object>;
		protected var _skinClass:Class;
		protected var _unLayoutX:Number;
		protected var _unLayoutY:Number;
		protected var _layouted:Boolean;
		protected var _enable:Boolean=true;
		public function Component(){
			
		}
		
		public function get enable():Boolean
		{
			return _enable;
		}

		public function set enable(value:Boolean):void
		{
			if(_enable===value)return
			if(!value)trace(this.parent,value);
				_enable=value;
			if(value){
				setEnable();
			}else{
				setUnable();	
			}
		}
		protected function setUnable():void{
			mouseChildren=mouseEnabled=false;
		}
		protected function setEnable():void{
			mouseChildren=mouseEnabled=true;
		}
		public function set layouted(value:Boolean):void
		{
			_layouted = value;
		}

		public function get unLayoutX():Number
		{
			return _unLayoutX;
		}
		public function get unLayoutY():Number
		{
			return _unLayoutY;
		}
		
		override public function set x(value:Number):void
		{
			super.x=value;
			if(!_layouted)_unLayoutX=value;
		}
		override public function set y(value:Number):void
		{
			super.y=value;
			if(!_layouted)_unLayoutY=value;
		}
		
		
		/**
		 * 
		 * @return 
		 * 
		 */
		public function get skinClass():Class{
			return _skinClass;
		}
		public function set skinClass(value:Class):void{
			_skinClass = value;
		}
		/**
		 * 
		 * 
		 */
		
		public function create():void{
			
		}
		/**
		 * 
		 * 
		 */
		public function attachSkin():void{
			
		}
		public function dispose():void{
			removeAllListener();
			removeAllChildren();
		}
		public function removeAllChildren():void{
			const num:int=numChildren;
			while(--num){
				const child:DisplayObject=removeChildAt(num);
				(child is Component)&&(child as Component).dispose();
			}
		}
		override public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void{
			!useWeakReference&&(_listeners?_listeners.push({a:type,b:listener}):_listeners=new Vector.<Object>);
			super.addEventListener(type, listener, useCapture, priority, useWeakReference);
		}
		override public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void{
			super.removeEventListener(type, listener, useCapture);
			const l:uint=_listeners.length;
			for(var i:uint=0;i<l;i++){
				const o:Object=_listeners[i];
				o.a==type&&o.b==listener&&_listeners.splice(i,1);
				break;
			}
		}
		public function removeAllListener():void{
			var o:Object;
			while(o=_listeners.pop())super.removeEventListener(o.a,o.b);
		}
	}
}